# Maintainer: Jonas Witschel <diabonas@archlinux.org>
pkgname=lib3mf-1
pkgver=1.8.1
pkgrel=6
pkgdesc='Implementation of the 3D Manufacturing Format file standard (legacy version)'
arch=('loong64' 'x86_64')
url='https://3mf.io/'
license=('BSD')
depends=('util-linux-libs' 'libzip' 'zlib') 
makedepends=('cmake')
checkdepends=('gtest')
source=("$pkgname-$pkgver.tar.gz::https://github.com/3MFConsortium/lib3mf/archive/v$pkgver.tar.gz"
        'lib3mf-1_use-system-gtest.patch::https://github.com/3MFConsortium/lib3mf/commit/cf679cc3736d4b22ce5711b5e81d28b7b6f4f4b0.patch'
        'lib3mf-1_link-libzip-and-zlib.patch::https://github.com/3MFConsortium/lib3mf/commit/093f2d95045dac25844936a72d5d8134a6265b20.patch'
        'lib3mf-1_CVE-2021-21772.patch::https://github.com/3MFConsortium/lib3mf/commit/6ac5f521f0a3e9f100814f515e380859c9a6ec46.patch')
sha512sums=('f7fd75bcb6472de1595a018e5add516d0d78ab0aee92462b686b77f8d2bef05270e7b737cb7e1d31fc5850815056e753874c2f9ec456a455e8461c4010fe914a'
            '59d107aafd512e183dba4ddb3d9f4f5d2f6942780c818f3a74a3b59361936fcc58283e4f38f91abe613321c165d301dfb9ae33d6dc28cbcc8590e807ded0506e'
            '97d8080c5affe299556d7a5c8a004ece0d143e6cbc685eddc57ecfd18f92cbf5795807829fcf76571df665cc1eb8da12ca5dcc5dfccd2f1b649d99341c5f0eb0'
            'a9f5acdbcf5edbcb06b8b52435b7fe687f5cb635bf8bf11bc72e6b142a4aa8192f26ef996dd768210d3917fde1b14cd69f65caacff41e23ddd7fa5a0550fed60')

prepare() {
	cd "${pkgname%-1}-$pkgver"
	patch --strip=1 --input="$srcdir/lib3mf-1_use-system-gtest.patch"
	patch --strip=1 --input="$srcdir/lib3mf-1_link-libzip-and-zlib.patch"
	patch --strip=1 --no-backup-if-mismatch --input="$srcdir/lib3mf-1_CVE-2021-21772.patch"
}

build() {
	cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_INSTALL_INCLUDEDIR="include/$pkgname" \
	      -DCMAKE_CXX_FLAGS="${CXXFLAGS} ${CPPFLAGS}" \
	      -DUSE_INCLUDED_LIBZIP=OFF -DUSE_INCLUDED_ZLIB=OFF -DUSE_INCLUDED_GTEST=OFF \
	      $( ((CHECKFUNC)) || echo -DLIB3MF_TESTS=OFF) \
	      -B build -S "$srcdir/${pkgname%-1}-$pkgver"
	make -C build
}

check() {
	cd build
	ctest
}

package() {
	cd build
	make DESTDIR="$pkgdir" install
	install -Dm644 "$srcdir/${pkgname%-1}-$pkgver/LICENSE" -t "$pkgdir/usr/share/licenses/$pkgname"
}
